using UnityEngine;
using UnityEngine.Audio;

public class AudioControl : MonoBehaviour
{
	public car car1;

	public GearBox GB;

	public skidMarks[] SM;

	public AudioMixer masterMixer;

	public float maxPitch = 2.4f;

	private float maxEngineLevel;

	private float maxSkidLevel;

	private float maxRollLevel;

	private float EngineLevel;

	private float SkidLevel;

	private float RollLevel;

	private float enginePitch;

	private float procentPitch;

	private AudioSource[] aSources;

	private bool skidPlay;

	private bool oldConrolled;

	private void Start()
	{
		aSources = GetComponents<AudioSource>();
		for (int i = 0; i < aSources.Length; i++)
		{
			if (car1.controlled)
			{
				aSources[i].Play();
			}
			else
			{
				aSources[i].Stop();
			}
		}
	}

	private void Update()
	{
		skidPlay = false;
		if (car1.controlled)
		{
			procentPitch = Procents(maxPitch - 1f, 1f);
			aSources[2].pitch = 1f + procentPitch * ProcentOfValue(GB.speed, 100f);
			if ((double)(GB.speed / 20f) < 0.7)
			{
				aSources[2].volume = GB.speed / 20f;
			}
			aSources[0].pitch = GB.currentPitch / 2f;
			if (!GB.shiftinGear && aSources[0].volume < GB.currentPitch)
			{
				aSources[0].volume += 0.05f;
			}
			if (GB.shiftinGear && (double)aSources[0].volume > 0.2)
			{
				aSources[0].volume -= 0.05f;
			}
			for (int i = 0; i < SM.Length; i++)
			{
				if (SM[i].skid)
				{
					skidPlay = true;
					break;
				}
			}
			if (skidPlay)
			{
				if (!aSources[1].isPlaying)
				{
					aSources[1].Play();
				}
			}
			else
			{
				aSources[1].Stop();
			}
			for (int j = 0; j < SM.Length; j++)
			{
				if (SM[j].bump)
				{
					aSources[3].volume = SM[j].bumpVolume;
					aSources[3].pitch = SM[j].bumpPitch;
					if (!aSources[3].isPlaying)
					{
						aSources[3].Play();
					}
				}
			}
		}
		if (!car1.controlled && oldConrolled)
		{
			for (int k = 0; k < aSources.Length; k++)
			{
				aSources[k].Stop();
			}
		}
		if (car1.controlled && !oldConrolled)
		{
			for (int l = 0; l < aSources.Length; l++)
			{
				aSources[l].Play();
			}
		}
		oldConrolled = car1.controlled;
	}

	private float Procents(float value, float procents)
	{
		return value / 100f * procents;
	}

	private float ProcentOfValue(float firstValue, float secondValue)
	{
		return (int)(firstValue / (secondValue / 100f));
	}
}
